package main

import (
	"os"
	"os/signal"
	"syscall"

	"gitee.com/sillyman/mixlog"
)

// listingSignal 监听进程幸好
func goListingSignal(exitFunc func()) {
	quitCh := make(chan os.Signal, 1)
	// 监听指定信号 ctrl+c kill
	signal.Notify(quitCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGUSR1, syscall.SIGUSR2)
	go func() {
		for sig := range quitCh {
			switch sig {
			case syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT:
				mixlog.Infof("捕获到一个退出进程信号: %s，程序将有序的退出……", sig.String())
				exitFunc()
				// case syscall.SIGUSR1:
				// case syscall.SIGUSR2:
				// default:
				// 	log.Printf("[I] 捕获到一个进程信号: %s，不做任何处理", sig.String())
			}
		}
	}()
}
